﻿using SwenAssigment.DBClasses;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SwenAssigment
{
    public partial class AssignDuty : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //system
        }

        protected void Calendar1_SelectionChanged(object sender, EventArgs e)
        {
            //system
        }

        protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
        {

        }

        protected void Button8_Click(object sender, EventArgs e)
        {
            if (ListBox7.SelectedIndex > -1)
            {
                if (AddorNo(ListBox8, ListBox7.SelectedValue) == true)
                {
                    ListBox8.Items.Add(ListBox7.SelectedItem);
                    ListBox8.SelectedIndex = 0;
                }
                else
                {
                    //cannot add
                }
            }
        }

        protected void Calendar1_DayRender1(object sender, DayRenderEventArgs e)
        {
            if (e.Day.IsOtherMonth)
            {
                e.Day.IsSelectable = false;
                e.Cell.Text = "";
            }
        }

        protected void Calendar1_SelectionChanged1(object sender, EventArgs e)
        {
            TextBox1.Text = Calendar1.SelectedDate.ToString("dd/M/yyyy");
        }
        protected Boolean AddorNo(ListBox n, String value)
        {
            foreach (ListItem s in n.Items)
            {
                if (s.Value == value)
                {
                    return false;
                }
            }
            return true;
        }
        protected void Button3_Click(object sender, EventArgs e)
        {
            if (ListBox1.SelectedIndex > -1)
            {
                if (AddorNo(ListBox2, ListBox1.SelectedValue) == true)
                {
                    ListBox2.Items.Add(ListBox1.SelectedItem);
                    ListBox2.SelectedIndex = 0;
                }
                else
                {
                    //cannot add
                }
            }
        }

        protected void Button4_Click(object sender, EventArgs e)
        {
            if (ListBox2.SelectedIndex > -1)
            {
                ListBox2.Items.Remove(ListBox2.SelectedItem);
            }
        }

        protected void Button9_Click(object sender, EventArgs e)
        {
            //ClientScriptManager cs = Page.ClientScript;
            if (ListBox2.Items.Count == 0 || ListBox4.Items.Count == 0 || ListBox6.Items.Count == 0 || ListBox8.Items.Count == 0)
            {
                //cs.RegisterStartupScript(this.GetType(), "PopupScript", "alert('For each duty type at least assign a staff');", true);
                Label1.Text = "*For each duty type at least assign a staff";
                Label1.Visible = true;
            }
            else
            {
                String status = Duty.CreateDuty(Calendar1.SelectedDate, ListBox2, ListBox4, ListBox6, ListBox8);
                if (status == "ok")
                {
                    //cs.RegisterStartupScript(this.GetType(), "PopupScript", "alert('Created');window.location.href = 'AssignDuty.aspx';", true);
                    Label1.Text = "*Created";
                    Label1.Visible = true;
                    TextBox1.Text = "";
                    ListBox2.Items.Clear();
                    ListBox4.Items.Clear();
                    ListBox6.Items.Clear();
                    ListBox8.Items.Clear();
                }
                else
                {
                    //cs.RegisterStartupScript(this.GetType(), "PopupScript", "alert('"+status+"');", true);
                    Label1.Text = "*" + status;
                    Label1.Visible = true;

                }
            }
        }

        protected void Button6_Click(object sender, EventArgs e)
        {
            if (ListBox5.SelectedIndex > -1)
            {
                if (AddorNo(ListBox6, ListBox5.SelectedValue) == true)
                {
                    ListBox6.Items.Add(ListBox5.SelectedItem);
                    ListBox6.SelectedIndex = 0;
                }
                else
                {
                    //cannot add
                }
            }
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            if (ListBox3.SelectedIndex > -1)
            {
                if (AddorNo(ListBox4, ListBox3.SelectedValue) == true)
                {
                    ListBox4.Items.Add(ListBox3.SelectedItem);
                    ListBox4.SelectedIndex = 0;
                }
                else
                {
                    //cannot add
                }
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            if (ListBox4.SelectedIndex > -1)
            {
                ListBox4.Items.Remove(ListBox4.SelectedItem);
            }
        }

        protected void Button5_Click(object sender, EventArgs e)
        {
            if (ListBox6.SelectedIndex > -1)
            {
                ListBox6.Items.Remove(ListBox6.SelectedItem);
            }
        }

        protected void Button7_Click(object sender, EventArgs e)
        {
            if (ListBox8.SelectedIndex > -1)
            {
                ListBox8.Items.Remove(ListBox8.SelectedItem);
            }
        }

        protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
    }
}